<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;

class Admin extends Controller
{
    //初始化的方法
    public function _initialize()
    {
        //判断session是否存在
        if (!Session::has('admin')) {
            //跳转到 登陆页
            $this->error('请先登录...','admin/login/index', 1,1);
//            return $this->redirect("login/index.html");
        }

//        权限过滤
        $request = Request::instance();
        $mname = strtolower($request->controller()); //获取控制器名
        $aname = strtolower($request->action()); //获取方法名

        $nodelist = Session::get('admin')['nodes'];
        $mnames = array_column($nodelist, 'mname');
        $anames = array_column($nodelist, 'aname');

        if (Session::get('admin')['username'] !== 'admin') {
            if (!in_array($mname,$mnames)) {
                $this->error('无权访问!');
                exit;
            } else {
                if (!in_array($aname,$anames)) {
                    $this->error('无权访问!');
                    exit;
                }
            }
        }



    }

}
